package com.zhuiYun.project.api.sysUser.service.impl;

import com.zhuiYun.project.api.commodityType.entity.CommodityType;
import com.zhuiYun.project.api.menu.entity.MenuEntity;
import com.zhuiYun.project.api.order.model.CommorderModel;
import com.zhuiYun.project.api.sysUser.entiy.Power;
import com.zhuiYun.project.api.sysUser.entiy.SysUser;
import com.zhuiYun.project.api.sysUser.mapper.SysUserMapper;
import com.zhuiYun.project.api.sysUser.model.SysUserModel;
import com.zhuiYun.project.api.sysUser.service.SysUserService;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.BeanUtils;
import com.zhuiYun.project.util.ObjectUtils;
import com.zhuiYun.project.util.UploadFileUtils;
import com.zhuiYun.project.util.UserUtils;
import com.zhuiYun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import com.zhuiYun.project.util.pageUtil.PageResult;
import com.zhuiYun.project.util.validator.ValidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SysUserServiceImpl
 * @Description 用户管理服务类 用于实现用户的增删改查 登录 注销
 * @Author zcy
 * @Date 2023/3/24 16:15
 **/
@Service
@Slf4j
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    /**
     * 通过登录名获取用户信息
     * @param loginName
     * @return
     */
    @Override
    public SysUser getUserByLoginName(String loginName) {
        // 判断用户登录名是否为空
        if(ObjectUtils.isNotEmpty(loginName)){
            // 使用Example表达式查询登录人信息
            Example example = new Example(SysUser.class);
            example.createCriteria().andEqualTo("loginName",loginName);
            return sysUserMapper.selectOneByExample(example);
        }else{
            return null;
        }
    }
    /**
     * @description: 查询用户权限
     * @author: dongdong
     * @date: 2023/4/7 17:17
     * @param: [userId]
     * @return: com.zhuiYun.project.common.response.CommonResult
    **/
    @Override
    public CommonResult getUserPower(Integer userId) {
        return CommonResult.ok(sysUserMapper.getUserPower(userId));
    }

    /**
     * 更新登录成功后更新用户信息
     * @param sysUser
     */
    @Override
    public void updateUserByLogin(SysUser sysUser) {
        if(ObjectUtils.isNotEmpty(sysUser)&&ObjectUtils.isNotEmpty(sysUser.getId())){
            sysUserMapper.updateByPrimaryKeySelective(sysUser);
        }
    }
    /**
     *
     * 新增用户
     * @param sysUserModel
     * @return
     * @throws CommonException
     */
    @Override
    public CommonResult saveUser(SysUserModel sysUserModel, MultipartFile avatar) throws CommonException {
        // 验证参数的规范性
        if(ValidatorUtil.ValidationObject(sysUserModel)){

            if(avatar!=null){
                String s = UploadFileUtils.uploadFile(avatar);
                log.info("员工添加-图片id-"+s);
                sysUserModel.setAvatarId(s);
            }
            sysUserModel.setUserFlag(0);
            SysUser sysUser = new SysUser();
            // sysUserModel 对象拷贝成 sysUser
            BeanUtils.copy(sysUserModel, sysUser);
            // 设置密码
            sysUser.setUpdateTime(new Date());
            sysUser.setCreateTime(new Date());
            sysUser.setPassword(bCryptPasswordEncoder.encode("123456"));
            int i = sysUserMapper.insertSelective(sysUser);
            if(i>0){
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        }else{
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }

    /**
     * 修改用户信息
     * @param sysUserModel
     * @return
     */
    @Override
    public CommonResult updateUser(SysUserModel sysUserModel,MultipartFile avatar) throws CommonException {
        if(ObjectUtils.isNotEmpty(avatar)){
            String s = UploadFileUtils.uploadFile(avatar);
            if(ObjectUtils.isNotEmpty(s)){
                sysUserModel.setAvatarId(s);

            }
        }
        // 验证参数的规范性
            SysUser sysUser = new SysUser();
            // sysUserModel 对象拷贝成 sysUser
            BeanUtils.copy(sysUserModel, sysUser);
            int i = sysUserMapper.updateByPrimaryKeySelective(sysUser);
            if(i>0){
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
    }

    /**
     * 删除用户
     * @param id
     * @return
     */
    @Override
    public CommonResult delUserById(Integer id) throws CommonException {
        // 判断是否id为空
        if(ObjectUtils.isNotEmpty(id)){
            int i = sysUserMapper.deleteByPrimaryKey(id);
            // 判断是否操作成功
            if(i>0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else{
                return CommonResult.ok(EmErrorCode.DELETE_ERROR);
            }
        }else{
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    /**
     * 分页查询
     * @param sysUserModel
     * @param pageRequest
     * @return
     */
    @Override
    public CommonResult getUserByPage(SysUserModel sysUserModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("userFlag",2);
        // 设置模糊查询
        if(ObjectUtils.isNotEmpty(sysUserModel.getUserName())){
            criteria.andLike("userName","%"+sysUserModel.getUserName()+"%");
        }
        PageResult pageResult = MybatisPageHelper.getPageResult(sysUserMapper.selectByExample(example));
        return CommonResult.ok(pageResult);
    }

    @Override
    public CommonResult userPower(Integer userId, List<Integer> integers) {

        int i = sysUserMapper.delPower(userId);
        if(integers.size()>0){

            sysUserMapper.userPower(userId,integers);
        }
        return CommonResult.ok();
    }


    @Override
    public CommonResult getPowerByUser() {
        SysUser user = UserUtils.getUser();
        Map<String, Power> map = sysUserMapper.getPowerByUser(user.getId());
        return CommonResult.ok(map);
    }


    /**
     * @Author 小江
     * @Description //TODO 获取后台管理客服账号信息
     * @Date 10:31 2023/4/26
     **/
    @Override
    public CommonResult getUserById(Integer id) {
        SysUser sysUser = sysUserMapper.selectByPrimaryKey(id);
        return CommonResult.ok(sysUser);
    }

    /**
     * 分页查询
     * @param sysUserModel
     * @param pageRequest
     * @return
     */
    @Override
    public CommonResult getUserRetrieveByPage(SysUserModel sysUserModel, PageRequest pageRequest) {
        // 设置分页
        MybatisPageHelper.startPage(pageRequest);
        Example example = new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userFlag",2);
        // 设置模糊查询
        if(ObjectUtils.isNotEmpty(sysUserModel.getUserName())){
            criteria.andLike("userName","%"+sysUserModel.getUserName()+"%");
        }
        PageResult pageResult = MybatisPageHelper.getPageResult(sysUserMapper.selectByExample(example));
        return CommonResult.ok(pageResult);
    }
}
